Fundr = {
	root:"/cfcs/remote.cfc?",
	ajaxURL:function(cfc,method){
		return Fundr.root + "cfc=" + cfc + "&callMethod=" + method + "&method=run";
	},
	getInput:function(name){
		var result = jQuery('input[name='+name+']').val() || "";
		return result;
	},
	getSelect:function(name){
		var result = jQuery('select[name='+name+']').val() || "";
		return result;
	},
	getAjaxData:function($area){
		var data = {};
		$area.find('input,select').not('[type=checkbox],[type=radio]').each(function(){
			data[this.name] = jQuery(this).val() || "";
		});
		$area.find('input[type=checkbox]').each(function(){
			if(jQuery(this).is(':checked')){
				data[this.name] = jQuery(this).val() || "";
			}
		});
		$area.find('input[type=radio]').each(function(){
			var $this = jQuery(this);
			if(!(this.name in data)){
				data[this.name] = "";
			}
			if($this.is(':checked')){
				data[this.name] = $this.val();
			}
		});
		return data;
	},
	fixTopBar:function(){
		if(Fundr.body.scrollTop() > 0){
			Fundr.topbar.addClass('fixed');
			Fundr.body.addClass('fixed');
		} else {
			Fundr.topbar.removeClass('fixed');
			Fundr.body.removeClass('fixed');
		}
	}
};

Alert = {
	getSystemMessages:function(){
		jQuery.ajax({
			url:Fundr.ajaxURL("alertService","getMessages"),
			type:"POST",
			success:function(data){
				var alert = "";
				for(var i = 0; i < data.length; i++){
					if(i > 0){alert += "</br>";	}
					alert += data[i];
				}
				if(alert != ""){
					Alert.showMessage(alert);
				}
			}
		});
	},
	showValidation: function(message,left){
		var jFocused = jQuery("*:focus");
		// Alert.validationMessages.dialog('close');
		var vert = $(window).height() / 2;
		if(jFocused.length == 1){
			var scrollTop = jQuery(document).scrollTop();
			var offset = jFocused.offset().top;
			vert = offset - scrollTop - 40;
		}
		var showLeft = left || false;
		Alert.validationMessages.empty();
		Alert.validationMessages.html("<span class='validation'>"+message+"</span>");
		var width = ($(window).width() / 2);
		if(showLeft){
			width -= 626;
			Alert.validationMessages.dialog("option","show",{effect:"drop",direction:"right"});
		} else {
			width += 620;
			Alert.validationMessages.dialog("option","show",{effect:"drop",direction:"left"});
		}
		Alert.validationMessages.dialog("option","position",[width,vert]);
		Alert.validationMessages.dialog('open');
	},
	showMessage: function(message,left){
		// Alert.systemMessages.dialog('close');
		var showLeft = left || false;
		Alert.systemMessages.empty();
		Alert.systemMessages.html("<span class='instructions'>"+message+"</span>");
		var width = ($(window).width() / 2);
		if(showLeft){
			width -= 596;
			Alert.systemMessages.dialog("option","show",{effect:"drop",direction:"right"});
		} else {
			width += 520;
			Alert.systemMessages.dialog("option","show",{effect:"drop",direction:"left"});
		}
		var vert = ($(window).height() / 2) - 120;
		Alert.systemMessages.dialog("option","position",[width,vert]);
		Alert.systemMessages.dialog('open');
	},
};

var Topbar = {
	loadCreateLeftNav:function(){
		jQuery('td#top_bar-leftBottom').find('div.top_bar-bottomNav').text('Create a Fund');
		jQuery('a.top_bar-nav').removeClass('navHere');
		jQuery('a.nav-create').addClass('navHere');
		jQuery("div.leftArea").not('div#leftCreateDiv').fadeOut('fast',function(){
			jQuery('div#leftCreateDiv').fadeIn();
		});
	},
	loadBrowseLeftNav:function(){
		jQuery('td#top_bar-leftBottom').find('div.top_bar-bottomNav').text('Browse for Funds');
		jQuery('a.top_bar-nav').removeClass('navHere');
		jQuery('a.nav-browse').addClass('navHere');
		jQuery("div.leftArea").not('div#leftBrowseDiv').fadeOut('fast',function(){
			jQuery('div#leftBrowseDiv').fadeIn();
		});
	},
	createFund:function(){
		if(Valid.validate(jQuery("div#leftCreateDiv"))){
			jQuery.ajax({
				url:Fundr.ajaxURL("fundService","saveFund"),
				type:"POST",
				data:Fundr.getAjaxData(jQuery('div#leftCreateDiv')),
				success:function(data){
					console.log(data);
					Alert.getSystemMessages();
					if(+data == 1){
						location.reload();
					}
				},
				error:function(data){
					Alert.getSystemMessages();
				}
			});
		}
	},
	filterFunds:function(){
		jQuery.ajax({
			url:Fundr.ajaxURL("fundView","renderFunds"),
			type:"POST",
			data:Fundr.getAjaxData(jQuery('div#leftBrowseDiv')),
			success:function(data){
				Alert.getSystemMessages();
				jQuery('div#ScrollFunds').fadeOut('fast',function(){
					jQuery('div#ScrollFunds').html(data);
					jQuery('div#ScrollFunds').slideDown();
				});
			}
		});
	}
};

jQuery(document).ready(function(){
	Alert.validationMessages = jQuery('div#validationMessages').dialog({
		show:"drop",title:"Validation!",autoOpen:false,height:70,width:170,
		stack:false,draggable:false
	});
	Alert.systemMessages = jQuery('div#systemMessages').dialog({
		show:"drop",title:"Notification",autoOpen:false,height:70,width:170,
		stack:false,draggable:false
	});
	Alert.getSystemMessages();
	
	// initiate datepickers
	jQuery("input[validate*=date]").each(function(){
		jQuery(this).datepicker();
	});
	
	//initiate left nav submit
	jQuery("div#leftBrowseDiv").on('keyup',function(e){
		if(e.keyCode == 13){
			console.log(e.keyCode);
			Topbar.filterFunds();
		}
	});
});